package com.hm.service.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.hm.service.service.wechat.IWeChatPublic;
import jakarta.servlet.http.HttpServletRequest;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;


@RestController
@RequestMapping(value = "/MAIN", produces = {"application/json;charset=UTF-8"})
public class WCPS_000_000_002 {
    private static final Logger logger = LoggerFactory.getLogger(WCPS_000_000_002.class);


    private final IWeChatPublic iWeChatPublic;

    @Qualifier("OkHttpClient")
    public OkHttpClient okHttpClient;

    public WCPS_000_000_002(IWeChatPublic iWeChatPublic, OkHttpClient okHttpClient) {
        this.iWeChatPublic = iWeChatPublic;
        this.okHttpClient = okHttpClient;
    }


    //发送订阅通知
    @GetMapping(value = "/000_000_002")
    public String doPost(HttpServletRequest httpRequest) {
        String accessToken = iWeChatPublic.getAccessToken();
        //String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/bizsend?access_token=" + accessToken;
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;

        SendNoticeBean paramsBean = new SendNoticeBean();
        paramsBean.access_token = accessToken;
        paramsBean.touser = "oPEDd6TILEQvFzUfqI79aZzmnsaA";
        paramsBean.template_id = "UaMcZ_ihjQhydJUKCt_p1OO-blT_27CqyuVEjN74S4M";

        JSONObject data = new JSONObject();
        data.put("name", new JSONObject().put("value", "做一个身体和心灵都勇敢的人，趁着身体未老，心灵还透明!"));
        data.put("weather", new JSONObject().put("value", "阴天  19°c"));
        data.put("birthday", new JSONObject().put("value", "53   天"));
        paramsBean.data = data;

        String reqParams = JSON.toJSONString(paramsBean);
        RequestBody body = RequestBody.create(reqParams, MediaType.get("application/json; charset=UTF-8"));
        Request request = new Request.Builder()//
                .url(url)//请求连接
                .post(body)//请求体
                .build();//构建
        try (Response response = okHttpClient.newCall(request).execute()) {
            //同步请求
            ResponseBody resBody = response.body();
            if (resBody != null) {
                String jsonString = resBody.string();
                logger.error("下发通知："+jsonString);

            } else {
                logger.error("下发通知失败onResponse为null！！ ");
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return "1111";
    }

    class SendNoticeBean implements Serializable {
        public String access_token;//接收者（用户）的 openid
        public String touser;//所需下发的订阅模板id
        public String template_id;
        public JSONObject data;//模板内容，格式形如 { "key1": { "value": any }, "key2": { "value": any } }
    }


}
